class ScrollerItem extends eui.ItemRenderer {
    public rolename :eui.Label;
    public avatar:eui.Image;
    public bgi:eui.Image;
    public clothId:string; 
    public isSelect:eui.Image;
    public dataManager:DataManager = DataManager.getInstance();
    public skinSelectPanel:SkinSelectPanel = SkinSelectPanel.getInstance();
    public socket:SocketUtils = SocketUtils.getInstance();
    public flag :boolean
    public constructor() {
        super();
        this.skinName ="resource/eui_skins/ScrollerItem.exml";
        this.addEventListener(egret.TouchEvent.TOUCH_TAP,this.clickItem,this);       
    }

    protected dataChanged():void{
        if(this.data.id == this.dataManager.myConfig['currentSkin']){
            this.isSelect.visible = true
        }else{
            this.isSelect.visible = false
        }
        let flag = false;
        for(let i = 0;i<this.dataManager.myConfig['hadSkin'].length;i++){
            if(this.data.id == this.dataManager.myConfig['hadSkin'][i]){
                flag = true;
                break;
            }
        }
        this.flag = flag
        if(flag && this.data.grade === "S"){
            this.bgi.source = "teshuda@2x_png"
        }else if(flag && (this.data.grade === "A" || this.data.grade === "Default")){
            this.bgi.source = "huanznnxz@2x_png"
        }else if(!flag){
            this.bgi.source = "weizhi@2x_png"
        }
        // 将数据对应到组件上
        this.rolename.text = this.data.name;
        this.clothId = this.data.clothId;
        let imgLoader = new egret.ImageLoader();
        imgLoader.crossOrigin = "anonymous";
        imgLoader.load(this.dataManager.avatarRUrl+this.data.imgName+".png");
        imgLoader.once(egret.Event.COMPLETE, (evt: egret.Event)=> {
            if (evt.currentTarget.data) {
                let texture = new egret.Texture();
                texture.bitmapData = evt.currentTarget.data;
                this.avatar.source = texture;
            }
        }, this);

        
    }
    
    public clickItem(e){
        //换装     
        if(this.flag){
            this.skinSelectPanel.role.changeRole(this.data.clothId);
            this.skinSelectPanel.oxygen_label.text =this.data.oxygen>=0?"氧气 +"+ this.data.oxygen:"氧气 "+ this.data.oxygen;
            this.skinSelectPanel.revival_label.text =this.data.revival>=0?"复活 +"+ this.data.revival:"复活 "+ this.data.revival;
            this.skinSelectPanel.speed_label.text =this.data.speed>=0?"游速 +"+ this.data.speed:"游速 "+ this.data.speed;
            this.skinSelectPanel.des_label.text = this.data.des;
            this.skinSelectPanel.rolename_label.text = this.data.name;
            
            let imgLoader = new egret.ImageLoader();
            imgLoader.crossOrigin = "anonymous";
            imgLoader.load(this.dataManager.skinBackRUrl+this.data.backImg+".jpg");
            imgLoader.once(egret.Event.COMPLETE, (evt: egret.Event)=> {
                if (evt.currentTarget.data) {
                    let texture = new egret.Texture();
                    texture.bitmapData = evt.currentTarget.data;
                    this.skinSelectPanel.skinBg.source = texture;
                }
            }, this);
            if(this.data.id !== this.dataManager.myConfig['currentSkin']){
                this.socket.socket.emit("blue/skin/change",this.data.id);
            }
            
        }else{
            let fragmentNum = 0+"/"+this.data.piecesComp;
            let skinFragment = JSON.parse(this.dataManager.myConfig['skinFragment']);
            //console.log(skinFragment)
            for(let item in skinFragment){
                if(item == this.data.id){
                    fragmentNum = skinFragment[item]+"/"+this.data.piecesComp;
                }
            }
            this.skinSelectPanel.role.changeRole(this.data.clothId);
            this.skinSelectPanel.oxygen_label.text = "???";
            this.skinSelectPanel.revival_label.text = "???";
            this.skinSelectPanel.speed_label.text = "???";

            var str: string = `<span>皮肤未拥有,当前碎片：</span>\n<span>${fragmentNum}</span>`;
            this.skinSelectPanel.des_label.textFlow = (new egret.HtmlTextParser).parser(str);
            this.skinSelectPanel.rolename_label.text = this.data.name;
            this.skinSelectPanel.skinBg.source = "putong_jpg";
            
        }
    }
}

